package pakete2;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Dimension;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Vector;
import java.awt.Point;

public class EI_TxartelaJaso extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JComboBox jComboBox = null;
	
	private JLabel jLabel1 = null;

	private JButton jButton = null;
	
	private KudeatzaileInterfazea kud = null;  //  @jve:decl-index=0:
	
	private EI_SegurtasunArduraduna aita;

	/**
	 * @param owner
	 */
	public EI_TxartelaJaso(EI_SegurtasunArduraduna aita) {
		super(aita, true);
		this.aita = aita;

		initialize();
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(325, 100);
		this.setLocation(new Point(400, 250));
		this.setContentPane(getJContentPane());
		this.setTitle("Txartela desgaitu");
		
		
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel1 = new JLabel();
			jLabel1.setText(" ");
			jLabel = new JLabel();
			jLabel.setText("Txartelaren kodea sartu:	");
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			Box box = Box.createHorizontalBox();
			Box box2 = Box.createVerticalBox();
			
			box.add(jLabel);
			box.add(this.getJComboBox());

			
			

			box2.add(getJButton());
			box2.add(jLabel1);
			
			
			jContentPane.add(box, BorderLayout.NORTH);
			jContentPane.add(box2, BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JComboBox getJComboBox() {
		try {
			kud = (KudeatzaileInterfazea) Naming.lookup("rmi://localhost/kudeatzailea");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Vector<String> txartelKodeak = null;
		try {
			txartelKodeak = kud.getTxartelak();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (jComboBox == null) {
			jComboBox = new JComboBox(txartelKodeak);
		}
		jComboBox.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				jLabel1.setText("");
				aita.eguneratu();
			}
		});
			
		return jComboBox;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton("Desgaitu");
			jButton.setEnabled(true);
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					KudeatzaileInterfazea kud;
					Integer x=0;
					try {
						kud = (KudeatzaileInterfazea) Naming.lookup("rmi://localhost/kudeatzailea");
						
						
						String txartelkod = (String) jComboBox.getSelectedItem();
						
							try {
								x=kud.desgaituTxartela(txartelkod);
							} catch (SQLException e1) {
								// TODO Auto-generated catch block
								//e1.printStackTrace();
								}
							if (x == 1) {
								JOptionPane.showMessageDialog(null, "txartela desgaitu da!");
							}
							else if (x == 0){
								JOptionPane.showMessageDialog(null, "dagoeneko desgaitutako txartela!");
							}
							else JOptionPane.showMessageDialog(null, "errorea txartela desgaitzean");
							setVisible(false);
							aita.eguneratu();
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}		
					
				}
			});
		}
		return jButton;
	}

}  //  @jve:decl-index=0:visual-constraint="0,0"
